package myJs.cps

import japgolly.scalajs.react.CtorType.ChildArg
import japgolly.scalajs.react.ScalaFnComponent
import myJs.myPkg.reactHookForm._
import japgolly.scalajs.react.vdom.all._

import scala.scalajs.js
import myJs.Implicits._
import japgolly.scalajs.react.React.Fragment

import scala.scalajs.js.JSON

object FormGroupInlineCp {

  case class Props(
      names: List[String],
      show: Boolean = true,
      key: String = ""
  )

  val Component = ScalaFnComponent.withChildren[Props] { (props,pC) =>
    val formState = ReactHookForm.useFormState(options =
      UseFormStateOptions(
        name = props.names
      )
    )

    val displayStr = if (props.show) "block" else "none"

    div(
      display := displayStr,
      classSet1(s"form-group ", "has-error" -> props.names.exists(formState.errors.deepDefinedAt(_))),
      pC
    )

  }

  def apply(
      names: List[String],
      show: Boolean = true,
      key: String = ""
  )(children: ChildArg*) = {
    if(key==""){
      Component(Props(names, show, key))(children:_*)
    }else{
      Component.withKey(key)(Props(names, show, key))(children:_*)
    }

  }

}
